package engine.newUi
{
	import engine.control.loading.MultLoader;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.data.Record;
	import engine.data.ResPath;
	import engine.data.RoleVo;
	import engine.data.SceneObject;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.flag.PlayerActionFlag;
	import engine.flag.ResourcesPath;
	import engine.guide.GuideManage;
	import engine.logic.DoubleClickManager;
	import engine.logic.event.LogicEvent;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneManager;
	import engine.tip.TipsMainLogic;
	import engine.util.FilterUtil;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import newx.StageInfo;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.window.NxWindowEvent;
	import newx.com.loader.SwfLoader;
	import newx.utils.BitmapAPI;
	import newx.utils.MouseHandle;

	/**
	 *布阵逻辑 
	 * @author yanghr
	 * 
	 */	
	public class EmbattleLogic
	{
		private static var _self:EmbattleLogic;
		private var _ui:EmBattleUI;
		private var _bindManage:BindManage
		/**
		 *NxViewGroup属性名 
		 */		
		private const CELL:String="cell";
		private const BG:String	=	"bg_mc";
		private const BOTTOM:String=	"bottomMc";
		private const DATA:String="data";
		private const NAME:String="titleTxt2";
		private const INDEX:String="index";
		private const PRIORITY:String="priority";
		private const PRIO_LABEL:String="ys_mc";
		private const IS_PRE:String="is preembattle";
		private const IS_LOADING:String="isloading";
		
		/**
		 *服务器属性名 
		 */
		private const EMBATTLE_LV:String="LineupLevel";
		
		private const SET_UP:String="(启用中)";
		/**
		 *上阵人格子数 
		 */		
		private const EMBATTLE_LEN:int=9
		
		private const NORMAL:int=1;
		private const	SELECTED:int=2;
		private const EVOKE:int=3;
		private const DISABLE:int=4;
		
		private const BOTTOM_NORMAL:int=3,
						BOTTOM_ROLL:int=1,
						BOTTOM_DISABLE:int=2;
		/**
		 *上锁 
		 */		
		/**
		 *普通 
		 */		
		private const NORMAL_FRAME:int=2;
		private const BATT_NORMAL:int=1,BATT_ROLL:int=2,LOCK_FRAME:int=3;
		//遍历顺序
		private const rank:Array=[2,5,8,1,4,7,0,3,6];
		private const TEMP_ARR:Array=["BuZhenStr_01","BuZhenStr_02","BuZhenStr_03"];
		private const toChin:Array=["BuZhenStr_04","BuZhenStr_05","BuZhenStr_06","BuZhenStr_07","BuZhenStr_08","BuZhenStr_09","BuZhenStr_10"]
		/**
		 *最大上阵人数 
		 */		
		private const MAX_EMBA_COUNT:int=5
		/**
		 *计算优先出手 
		 */			
		private var _priorityFlag:int=0;
		private var _initBG:Boolean	;
		
		private var _battleLvl:int;
		private var _embaCount:int;
		private var _preLastFlag:int;
		private var _bindEvokeFlag:int=-1;
		private var _bindCountFlag:int=-1;
		private var _bindBattleLevelFlag:int	=	-1;
		private var _bindFlag:int=-1;
		private var _past_flag:int=-1;
		private var _blindFlag:int=-1;
		private var _blagLvlFlag:int=-1;
		private var _rollMc:MovieClip;
		private var _partnerUidMap:Object=new Object;
		
		private var _upBtnGuideIndex:int;
		/**
		 *viewObject array   储存阵法
		 */		
		private var _tabArr:Array=[];
		/**
		 *role data 
		 */		
		private var _roleArr:Array=[];
		
		/**
		 *objKey到实际位置映射表 
		 */		
		private var _real_Map:Object=new Object;
		private var _waitingforUpdata:Boolean	=	true;
		
		private var _configArrObj:Object;
		private var _currentConfigArr:Array;
		private var _currentUnLockarr:Array;
		private var _listArr:Array=[];
		private var _role:RoleVo;
		private var _tips:TipsMainLogic;
	
		private var _model : ModelLocator;
		private var _mouseHandle:MouseHandle;
		private var _prearr:Array
		private var _emArr:Array
		private var _nowCountInEmbattle:int;
		private var _proirity:int;
		private var _currentSeleted:int=-1;
		private var _showCount:int	=	0;
		private var _radio:Boolean;
		
		private var _rollOverIcon:DisplayObject;

		
		private var _channelMC:MovieClip;
		public static function getInstance():EmbattleLogic
		{
			if(_self==null)
			{
				_self=new EmbattleLogic();
			}
			return _self;
		}
		
		public function get ui():EmBattleUI
		{
			if(_ui==null)
			{
				_ui=new EmBattleUI();
			}
			return _ui
		}
		 public function Init():void
		{
			 _bindManage	=	BindManage.getInstance();
			 _model	=	ModelLocator.getInstance();
			 _role	=	_model.scene.Role;
			 _mouseHandle	=	MouseHandle.getInstance();
			 _tips	=	TipsMainLogic.instance;
			var i:int
			_prearr=[];
			var mc:MovieClip
			_ui.uplevelBtn.label=StaticIni.getSwfLangStr("UIString_1000395")
			var len:int= _ui.preEmbaGroup.row*_ui.preEmbaGroup.col;
			for ( i = 0; i <len; i++) 
			{
				var cell:NxClip	=	new NxClip();
				cell.isCache = true;
				mc	=	_ui.preEmbaGroup.getItemByIndex(i) as MovieClip
				mc[INDEX]	=	i;
				mc[CELL]	=	cell;
				mc[IS_PRE]=true;
				mc["img01"].addChild(cell)
				_prearr.push(mc);
				mc.addEventListener(MouseEvent.MOUSE_UP,onUp)
				mc.addEventListener(MouseEvent.ROLL_OVER,cellTipsShow);
				mc.addEventListener(MouseEvent.ROLL_OUT,rollOut);
				mc.gotoAndStop(BATT_NORMAL)
				if(i==0)
				{
					var nx:MovieClip =ResManager.newResClass("embattlePilot2") as MovieClip;
					nx.visible=false;
					nx.x=30
					nx.y=20;
					mc["nx"] = nx;
					mc.addChild(nx);
				}
//				mc.stop()
			}
			mc =_ui.preEmbaGroup.getItemByIndex(0) as MovieClip
			mc.parent.setChildIndex(mc,4);
//			_channelMC =ResManager.newResClass("PartnerInviteUIPic") as MovieClip
			_emArr=[];
//			len=_ui.emBattleGroup.row*_ui.emBattleGroup.col
			_upBtnGuideIndex = StaticIni.getIniVar("GuideInfo","btnSort").split(",").indexOf("btn_10");
//			var btn
			//初始化列表
			//去掉列表
			_ui.evokeBattleBtn.visible=false;
			ui.listLab.visible=false
			
			_configArrObj=StaticIni.getIniObj("LineupInfo")
			showCount	=	1;
			radioSelect	=	true;
			_bindManage.BindProp(_model.scene.Role,"TotalSpeedRateAdd",this,"proirityCount");
//			_ui.mcBG4.source=ResPath.JPG+"embattleBG.jpg";
//			ui.lab_1.htmlStrByBorder=StaticIni.getSwfLangStr(TEMP_ARR[0])
//			ui.lab_2.htmlStrByBorder=StaticIni.getSwfLangStr(TEMP_ARR[1])
//			ui.lab_3.htmlStrByBorder=StaticIni.getSwfLangStr(TEMP_ARR[2]);
		}
		 private function rollOut(e:Event):void
		 {
			 rollMc=null;
			 TipsMainLogic.instance.hide()
		 }
		private function initEmbattle():void
		{
			var mc:MovieClip;
			for (var  i:int = 0; i < EMBATTLE_LEN; i++) 
			{
				var cell2:NxClip=new NxClip();
				cell2.isCache = true;
				mc	=	getEmbattleItemByindex(i)||setEmbattleItemByindex(i,_ui.embattle["embItem_"+i])//_ui.emBattleGroup.getItemByIndex(i)
				mc[INDEX]=i;
				mc[CELL]	=	cell2;
				cell2.mouseEnabled=cell2.mouseChildren=false;
				mc[BG]["img2"].addChild(mc[CELL]);
				mc[BOTTOM].gotoAndStop(BOTTOM_NORMAL);
				mc[IS_PRE]	=	false;
				mc.addEventListener(MouseEvent.ROLL_OVER,cellTipsShow);
				mc.addEventListener(MouseEvent.ROLL_OUT,rollOut)
				_emArr.push(mc);
//				mc.gotoAndStop(BATT_NORMAL);
				_ui.embattle["ItemEffect_"+i].addEventListener(MouseEvent.ROLL_OVER,cellTipsShow);
				_ui.embattle["ItemEffect_"+i].addEventListener(MouseEvent.ROLL_OUT,rollOut);
				_ui.embattle["ItemEffect_"+i].addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandle);
				_ui.embattle["ItemEffect_"+i].addEventListener(MouseEvent.MOUSE_UP,mouseUpHandle);
				
				mc.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandle);
				mc.addEventListener(MouseEvent.CLICK,click);
				var nx:NxClip=new NxClip();
				var name:String ="";
				name="embattlePilot2"//(i==4 ||i==5)?"embattlePilot1":"embattlePilot2"
				nx.source =name//ResManager.newResClass(name) as MovieClip;
				nx.visible=false;
				nx.x=60
				nx.y=110;
				mc["nx"] = nx;
			    mc.addChild(nx);
				//				mc.stop()
			}
		}
		private function getEmbattleItemByindex(index:int):MovieClip
		{
			return _ui["embattle"+(index+1)];
		}
		private function setEmbattleItemByindex(index:int,mc:MovieClip):MovieClip
		{
			 _ui["embattle"+(index+1)]=mc;
			 return mc;
		}
		/**
		 *function(mc:MovieClip,index:int):void 
		 * @param f
		 * 
		 */		
		private function emBattleItemEachFunc(f:Function):void
		{
			for (var i:int = 0; i < EMBATTLE_LEN; i++) 
			{
				f(getEmbattleItemByindex(i),i);
			}
			
		}
		private function initBG():void
		{
			ui.visible=false
			const url:String=ResPath.SWF+"embattle.swf";
			MultLoader.getInstance().load([url],true,function():void
			{
				_initBG=true
				var mc:MovieClip=MultLoader.getInstance().getSourceByUrl(url);
				_ui.embattle.addChild(mc);
				_ui.embattle=mc;
				_rollOverIcon=_ui.embattle.getChildByName("tipsIcon");
				_tips.objRollshowTips(	_rollOverIcon,function():String{return StaticIni.getSwfLangStr("UIString_1000568")});
				initEmbattle();
				ui.visible=true;
				BeforeShow();
			}
			)
		}
		 public function BeforeShow():void
		{
			 _isFright=false;
			 if(!SceneManager._itemIconLoad)
			 {
				 _model.addEventListener(LogicEvent.ITEMICONLOADED,loaded);
				 function loaded():void
				 {
					 BeforeShow();
					 _model.removeEventListener(LogicEvent.ITEMICONLOADED,loaded)
				 }
			 }
//			 for each (var i:MovieClip in _prearr) 
//			 {
//				 i.gotoAndStop(1);
////				 i.setChildIndex(i[CELL],i.numChildren-1);
//			 }
			 if(!_initBG)
			 {
				 initBG();
				 return ;
			 }
			 if(currentSeleted	<	0)
			 {
				 currentSeleted	=	0;
			 }
			_tabArr[battleLen]	=	null;
		
			addAllListener()
			
//			radioSelect		=	false;
			update()
		
		}
		 /**当前位置显显示图标的位置**/
		 private var currpos:MovieClip;
		 /**是否进行下一个进到**/
		 private var isnext:Boolean;  //是否进行下一个引导		
		 /**引导的当前步骤位置（新手引导 -1    全部正确0     主角不在正确位置 1         点击主角 2       点击挪到主角正确位置3       伙伴不在正确位置4       点击伙伴5       点击挪到伙伴正确位置6）**/
		 private var steerStep:int;
		 /**玩家位置是否正确 **/		 
		 private var _roleIsRight:Boolean=true;
		 /**伙伴位置是否正确**/
		 private var _buddyIsRight:Boolean=true;
		 /**当前玩家所在的MC**/
		 private var _currRolemc:MovieClip;
		 /**当前伙伴所在的MC**/
		 private var _currBuddymc:MovieClip;
		 
		 private var _isFrist:Boolean =false;
		 
		 
		 private var _isFright:Boolean;
		 /**
		  *引导 执行函数 
		  * @param index 当前所处的步骤位置
		  * @param mc   当前所在的mc
		  * 
		  */		 
		 private function administerHanle(index:int,mc:MovieClip=null,a:Boolean=false):void
		 {
			 if(!isCheck)return;
			 switch(index)
			 {
				 case -1:
					 if(currpos!=null)
					 {
						 currpos["nx"].visible=false;
					 }
					 if(mc&& mc["nx"]&& mc[DATA])
					 {
						 mc["nx"].visible=true;
					 }
					 break;
				 case 0:
					 if(currpos!=null)
					 {
						 currpos["nx"].visible=false;
					 }
					 break;
				 case 1:
					 if(currpos!=null)
						 currpos["nx"].visible=false;
					
					 var name:String="embattlePilot2"
					 if(getIsRight())return;
					 mc["nx"].source =name;
					 currpos =mc;
					 currpos["nx"].visible=true;
					 break;
				 case 2:
					 if(currpos!=null)
					 	currpos["nx"].visible=false;
					 currpos =_ui.embattle["embItem_"+5];
					 currpos["nx"].source ="embattlePilot1"
					 currpos["nx"].visible=true;
					 break;
				 case 3: 
					 swapKit(mc,_ui.embattle["embItem_"+5]);
					 break;
				 case 4:
					 if(currpos!=null)
						 currpos["nx"].visible=false;
					 var name1:String="embattlePilot2"
					 mc["nx"].source =name1;//ResManager.newResClass(name1) as MovieClip;
					 currpos =mc;
					 currpos["nx"].visible=true;
					 break;
				 case 5:
					 if(currpos&&currpos["nx"])
						 currpos["nx"].visible=false;
					 currpos =_ui.embattle["embItem_"+4];
					 currpos["nx"].source ="embattlePilot1"
					 currpos["nx"].visible=true;
					 break;
				 case 6:
					 swapKit(mc,_ui.embattle["embItem_"+4]);
					 break;
			 }

		 }
		 
		 /**
		  *检测是不是开始第一次引导 
		  * @return 
		  * 
		  */		 
		 private function checkFristOpen():Boolean
		 {
			 var currStep:String="";
			 currStep =GuideManage.getInstance().curstep;
			 if(currStep == "91008")
			 {
				 if( _prearr[0][DATA])
				 {
					 steerStep=-1;
					_isFrist=true;
					administerHanle(steerStep,_prearr[0]);
					SystemSender.getInstance().CustomMsg( CustomToServerFlag.CLIENT_CUSTOMMS_SET_GUIDE_STEP, StaticIni.getIniVar( currStep, "NextStep" ), 1 );
					
					GuideManage.getInstance().getNextGuide( StaticIni.getIniVar( currStep, "NextStep" ) );
				 	return true;
				 }
				
			 }
			 return false;
		 }
		 /**
		  * 打开界面或者更新阵法后掉用的  人物引导 
		  * @param index   当前位置
		  * @param isRole  是不是主角
		  * 
		  */		 
		 private function updatesteer(index:int,isRole:Boolean,obj:*):void
		 {
			 if(!isCheck)return ;
			 if(checkFristOpen())
			 {
				 administerHanle(steerStep,_prearr[0]);
				 return;
			 }
			 if(currpos )
			 {
				 currpos["nx"].visible=false;
			 }
			 var mc:MovieClip = _ui.embattle["embItem_"+index];
			 isnext =true;
			 if(isRole)
			 {
				
				 _currRolemc =mc 
				 _currRolemc[DATA]=obj
				 _roleIsRight=true;
				 if(index != 5)
				 {
					 steerStep=1;
					 administerHanle(steerStep,_currRolemc);
					 isnext =false;
					 _roleIsRight=false;
					 return;
				 }
				if(_currRolemc[DATA]==null)
					_currRolemc[DATA] =obj
				checkNext(_currRolemc[DATA])
			
			 }
			 else
			 {	
				 _currBuddymc=mc;
				 _currBuddymc[DATA]=obj;
				 _buddyIsRight=true
				 if(index != 4 &&isnext)
				 {
					 steerStep=4;
					 administerHanle(steerStep,_currBuddymc);
					 _buddyIsRight=false;
					return ;
				 }
				 if(_currBuddymc[DATA]==null)
					 _currBuddymc[DATA]=obj;
				 
				checkNext(_currBuddymc[DATA])
		
			 }
			 steerStep=0;
		 }
		 /**主角位置是不是正确**/
		 private function get rolePos():Boolean
		 {
			 if(_currRolemc)
			 {
				 if(_currRolemc[DATA]==null)
					 return _roleIsRight ;
			 }
			 return _roleIsRight;
		 }
		 /**伙伴位置是不是正确**/
		 private function get buddyPos():Boolean
		 {
			 if(_currBuddymc)
			 {
				 if(_currBuddymc[DATA]==null)
					 return true;
			 }
			 return _buddyIsRight;
		 }
		 /**
		  *是否需要检查玩家阵法是理想的状态 自己伙伴数<=1
		  * @return 
		  * 
		  */		 
		 private function get isCheck():Boolean
		 {
			 var view:Object = _model.scene.GetView(View.VIEWPORT_PARTNER).Properties
			 var parnetTimes:int=0;
			 for (var i:String in view) 
			 {
				 if(Number(i))
				 {
					 if(view[i].GetPropertiesEx("ConfigID"))
					 {
						 parnetTimes++;
					 }
				
				 }
			 }
			 return (parnetTimes<=1 && !_isFright)?true:false;
		 }
		 /**
		  * //等级=消耗;速度;阵点;阵法数量;人物等级;任务id 
		  * @param lvl
		  * @return 
		  * 
		  */		 
		 private function getConfigArr(lvl:int):Array
		 {
			 return String(_configArrObj[lvl]).split(";");
		 }
		 private function currentConfigSpeed():int
		 {
			 return _currentConfigArr[2];
		 }
		 private function currentConfigEmbattle():Array
		 {
			 return _currentConfigArr[0].split(",");
		 }
		 private function currentbattleSpeed():int
		 {
			 return _currentConfigArr[2];
		 }
		 /**
		  *从0开始 
		  * @return 
		  * 
		  */		 
		 public function get currentSeleted():int
		 {
			 return _currentSeleted;
		 }
		 
		 public function set currentSeleted(value:int):void
		 {
			 var mv:MovieClip=_listArr[_currentSeleted] as MovieClip;
			 if(currentSeleted>-1	&&	currentSeleted<_listArr.length &&	mv.currentFrame==SELECTED)
			 {
				 mv.gotoAndStop(NORMAL);
			 }
			 _currentSeleted = value;
			 _currentConfigArr=getConfigArr(value+1);
			 _currentUnLockarr=currentConfigEmbattle();
			 mv=_listArr[value] as MovieClip;
//			 if(mv.currentFrame	==	NORMAL)
//			 {
//				 mv.gotoAndStop(SELECTED);
//			 }
		 }
		 public function get showCount():int
		 {
			 return _showCount;
		 }
		 public function set showCount(value:int):void
		 {
			 var starIndex:int	=	_showCount;
			 _showCount=value;
			 var mc:MovieClip;
//			 trace("EmbattleLogic.showCount(value)",starIndex,showCount);
//			 for (var i:int = starIndex; i < showCount; i++) 
//			 {
//				mc	=	_listArr[i] as MovieClip;
//				mc.gotoAndStop(NORMAL);
//			 }
//			 for (i = showCount; i < starIndex; i++) 
//			 {
//				 mc	=	_listArr[i] as MovieClip;
//				 mc.gotoAndStop(DISABLE);
//			 }
		 }
		 private var posArr:Array=[];
		 private function getIsRight():Boolean
		 {
			 var num:int=0;
			 var friend:Array= _model.scene.GetRecordEx("LineupRecord").recArr;
			 for each(var i:Array in friend)
			 {
				 if(i[2]==-1 || i[2]==1)
				 {
					 num++;
				 }
			 }
			 if(friend.hasOwnProperty(5) &&friend.hasOwnProperty(4))
			 {
				 if(friend[5][2]==-1&&friend[4][2]==1 &&num==2)
				 {
					 if(currpos)
					 {
						 currpos["nx"].visible=false;
					 }
					 return true;
				 }
			 }
			
			 return false;
		 }
//		 private function checkUpdate(count:int,obj:Object):Boolean
//		 {
//			 if(!obj)return false;
//			 if(!_tabArr || _tabArr[count].lenth<1 )return true;
//			 for each(var i:Object in  _tabArr[count])
//			 {
//				 if((i is ViewObject ||i is RoleVo )&&obj.GetProperties("ConfigID") == i.GetProperties("ConfigID"))
//				 {
//					 return false;
//				 }
//			 }
//			 return true
//		 }
		 /**
		  *	添加
		  *	[0]
		  * [1]
		  * [2] 
		  */  
		 private function onaddPerHandle(PropertiesName:String,PropertiesValue:Array,data:*):void
		 {
			 if(getIsRight() && !_isFright)
			 {
				 _isFright=true;
			 }
//			 trace("EmbattleLogic.onaddPerHandle(PropertiesName, PropertiesValue, data)",PropertiesName,PropertiesValue,data);
			 var tabCount:int		=	PropertiesValue[0]-1;
			 var countInGroup:int	=	PropertiesValue[1];
			 var uid:String		= 	PropertiesValue[2];
			 var dd:Object;
			 showCount=Math.max(showCount,tabCount+1);
			 if( _tabArr[tabCount]	==	null)
			 {
				 _tabArr[tabCount]	=	[];
			 }
			 if(uid&&uid!="0")
			 {
				 if(uid	==	"-1")
				 {
					 dd	=	 _tabArr[tabCount][countInGroup]	=	_role;
//					 if( currentSeleted	==	tabCount    &&	dd	!=	null)
//					 {
//						 addItem(_emArr[countInGroup],dd)
////						 nowCountEnba=_nowCountInEmbattle+1
//					 }
					 updatesteer(countInGroup,true,dd)
				 }
				 else
				 {
					 dd	=	 _tabArr[tabCount][countInGroup]	=	_partnerUidMap[uid];
					 updatesteer(countInGroup,false,dd)
//					if( currentSeleted	==	tabCount    &&	dd	!=	null)
//					{
//						addItem(_emArr[countInGroup],dd)
////						nowCountEnba=_nowCountInEmbattle+1
//					}
				 }
				
			 }
			 else
			 {
				  _tabArr[tabCount][countInGroup]	=	null;
//				 if( currentSeleted	==	tabCount)
//				 {
//					 clearItem(_emArr[countInGroup])
////					 nowCountEnba=_nowCountInEmbattle-1
//				 }
			 }
			 if(_waitingforUpdata)
			 {
//				 _waitingforUpdata=false;
				 update();
			 }
		
			 else if(radioSelect)
			 {
				 updatPriority();
			 }
//			 else
//			 {
//				 updataTxt()
//			 }
		 }
		 
		 /**
		  *  取消
		  * @param taskId
		  */
		 private function onremovePerHandle(PropertiesName:String,PropertiesValue:Array,data:*):void
		 {
//			 trace("EmbattleControl.removePerHandle(PropertiesName, PropertiesValue, data)",PropertiesName,(PropertiesValue),data);
			 var tabCount:int		=	PropertiesValue[0]	-	1;
			 var countInGroup:int	=	PropertiesValue[1];
			 var data:Point		=	PropertiesValue[3];
			 if( _tabArr[tabCount]	==	null)
			 {
				 _tabArr[tabCount]	=	[];
			 }
			 else
			 {
				 _tabArr[tabCount][countInGroup]	=	null;
			 }
			 if( currentSeleted	==	tabCount)
			 {
				 clearItem(_emArr[countInGroup])
			 }
			 updataTxt()
		 }
		/**
		 *单选 
		 * @param value
		 * 
		 */		
		private function set radioSelect(value:Boolean):void
		{
			_ui.rankRadio.gotoAndStop(value+1);
			_radio=value;
		}
		private function get radioSelect():Boolean
		{
			return _radio
		}
		private function click(e:MouseEvent):void
		{
			trace("click")
			DoubleClickManager.getInstance().onClick(EmbattleLogic,e);
		}
		/**
		 * 添加事件
		 */		
		public function addAllListener():void
		{
			_ui.priorityLab.addEventListener(MouseEvent.ROLL_OVER,onOVerLable);
			_ui.priorityLab.addEventListener(MouseEvent.ROLL_OUT,onHideLable);
			with(_ui)
			{
			windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,		onClickClose);
			uplevelBtn.addEventListener(MouseEvent.CLICK,uplevelhandle)
			evokeBattleBtn.addEventListener(MouseEvent.CLICK,evokehandle);
			rankRadio.addEventListener(MouseEvent.CLICK,radioHandle);
			preEmbaGroup.addItemsEvent(MouseEvent.MOUSE_DOWN,mouseDownHandle)
//			emBattleGroup.addItemsEvent(MouseEvent.MOUSE_DOWN,mouseDownHandle)
//			emBattleGroup.addEventListener(MouseEvent.CLICK,click);
			DoubleClickManager.getInstance().addDoubleClick(EmbattleLogic,function (e:Event):void{trace("点击")},doubleClickHandle)
			}
			//绑定当前伙伴
			if( _bindFlag == -1 )
			{
				_bindFlag		=	_bindManage.BindView(_model.scene,View.VIEWPORT_PARTNER,OnAddPartner,OnRemovePartner);
			}
			//绑定当前阵法登记
			if(_bindBattleLevelFlag==-1)
			{
				_bindBattleLevelFlag	=	_bindManage.BindProp(_model.scene.Role,EMBATTLE_LV,this,"battlelevel");
			}
			//绑定当前阵法表格
			if(_blindFlag==-1)
			{
//				trace("EmbattleControl.addAllListener()阵法绑定");
				_blindFlag= _bindManage.BindRecord(_model.scene,"LineupRecord",onaddPerHandle,onremovePerHandle);
			}
			//绑定当前玩家等级
			if(_blagLvlFlag==-1)
			{
				_blagLvlFlag==_bindManage.BindProp(_role,ActorPropertyUI.LEVEL,this,"level")
			}
			//绑定当前启用阵法
			if(_bindEvokeFlag==-1)
			{
//				_bindEvokeFlag	=	_bindManage.BindProp(_model.scene.Role,"LineupID",this,"upBattle");
			}
			if(_bindCountFlag==-1)
			{
//				_bindCountFlag	=	_bindManage.BindProp(_model.scene.Role,"PartnerBox",this,"nowCountEnba");
			}
//			proirityCount	=	111;
//			battlelevel		=	11;
		}
		/**
		 * 删除事件 
		 */		
		public function removeAllListener():void
		{
			_ui.priorityLab.removeEventListener(MouseEvent.ROLL_OVER,onOVerLable);
			_ui.priorityLab.removeEventListener(MouseEvent.ROLL_OUT,onHideLable);
			with(_ui)
			{
			windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,		onClickClose);
			uplevelBtn.removeEventListener(MouseEvent.CLICK,uplevelhandle)
			evokeBattleBtn.removeEventListener(MouseEvent.CLICK,evokehandle);
			rankRadio.removeEventListener(MouseEvent.CLICK,radioHandle);
			preEmbaGroup.removeItemsEvent(MouseEvent.MOUSE_DOWN,mouseDownHandle)
//			emBattleGroup.removeEventListener(MouseEvent.DOUBLE_CLICK,doubleClickHandle);
			}
//			emBattleItemEachFunc(function(mc:MovieClip,i:int):void{
//				mc.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandle)
//			})
//			trace("EmbattleControl.removeAllListener()，阵法反绑定");
			_blindFlag= _bindManage.UnbindRecord("LineupRecord",_blindFlag);
			_bindEvokeFlag	=_bindManage.UnbindProp(_model.scene.Role,"LineupID",_bindEvokeFlag);
//			_bindCountFlag	=_bindManage.UnbindProp(_model.scene.Role,"PartnerBox",_bindCountFlag);
			_bindBattleLevelFlag	=_bindManage.UnbindProp(_model.scene.Role,EMBATTLE_LV,_bindBattleLevelFlag);
			_bindFlag		=	_bindManage.UnbindView(View.VIEWPORT_PARTNER,_bindFlag);
		}
		private function onOVerLable(e:MouseEvent):void
		{
			var str:String = StaticIni.getSwfLangStr("UIString_1000695");
			TipsMainLogic.instance.showString(str);
		}
		private function onHideLable(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		public function get rollMc():MovieClip
		{
			return _rollMc;
		}
		private var value_1:MovieClip ; 
		public function set rollMc(value:MovieClip):void
		{
			if(value)
			{
//				trace("当前滑过的组件"+value.name);
				
			}
			if(_rollMc)
			{
				if(_rollMc.currentFrame!=LOCK_FRAME)
				{
					_rollMc.gotoAndStop(BATT_NORMAL);
				}
				if(_rollMc.hasOwnProperty(BOTTOM))
				{
					_rollMc[CELL].filters = null;
					//				if(_mouseHandle.view)//在拖拽
					{
						_rollMc[BOTTOM].gotoAndStop(BOTTOM_NORMAL);
					}
				}
			}
			_rollMc = value;
			if(_rollMc)
			{
				if(_rollMc.currentFrame!=LOCK_FRAME)
				{
					_rollMc.gotoAndStop(BATT_ROLL);
				}
				if(_rollMc.hasOwnProperty(BOTTOM))
				{
					_rollMc[CELL].filters = [ FilterUtil.WhiteGlowFilter ];
					if(_mouseHandle.view&&_rollMc[DATA] is RoleVo&&	_mouseHandle.targetData[1][IS_PRE])//在拖拽
					{
						_rollMc[BOTTOM].gotoAndStop(BOTTOM_DISABLE);
					}
					else
					{
						_rollMc[BOTTOM].gotoAndStop(BOTTOM_ROLL);
					}
				}
			}
		}
		/**
		 * 
		 * @param e
		 * 
		 */		
		private function cellTipsShow(e:MouseEvent):void
		{
			var cell:MovieClip=e.currentTarget as MovieClip;
			if(cell.name.search("ItemEffect_") != -1)
			{
				cell=_ui.embattle[cell.name.replace("ItemEffect_","embItem_")];
			}
			rollMc=cell;
			var viewobj:ObjectData=cell[DATA];
			if(viewobj&&!(_mouseHandle.view))
			TipsMainLogic.instance.showByItem(TipsMainLogic.instance.getNormalPartnerItem(viewobj));
		}
		private function onUp(e:MouseEvent):void
		{
			var cell:MovieClip = e.currentTarget as MovieClip;
			if(cell)
			{
				if(_isFrist&&cell[DATA])
				{
					if( _prearr[0]["nx"].visible)
					{
						_prearr[0]["nx"].visible=false;
						
					}
					if(steerStep ==5)
					{
						administerHanle(steerStep, _prearr[0])
					}
				}
				
			}
			else
			{
				steerStep =-1
				// 新手引导
				administerHanle(steerStep, _prearr[0])
			}
			StageInfo.stage.removeEventListener(MouseEvent.MOUSE_UP,onUp);
		}
		private function mouseDownHandle(e:Event):void
		{
			
			var cell:MovieClip=e.currentTarget as MovieClip;
			
			var value:int =cell.name.indexOf("ItemEffect_");
			if(value!=-1)
			{
				var index :int  = cell.name.split("ItemEffect_")[1];
				cell = getEmbattleItemByindex(index);
			}
			trace("当前对象名字："+cell.name);
//			if(cell.name.search("ItemEffect_") != -1)
//			{
//				//				rollMc=_ui[cell.name.replace("embItem_","ItemEffect_")];
//				cell=_ui.embattle[cell.name.replace("ItemEffect_","embItem_")];
//				//				rollMc=cell ;
//			}
			if(_isFrist&&cell[DATA])
			{
				StageInfo.stage.addEventListener(MouseEvent.MOUSE_UP,onUp);
				if(cell[DATA] is RoleVo)
				{
					return;
				}
				if(_mouseHandle.targetData)return ;
			}
			var sourceArr:Array;
			if(_prearr.indexOf(cell)>=0)
			{
				sourceArr=_prearr;
			}
			else
			{
				sourceArr=_emArr;
			}
			if(cell[DATA])
			{
				var dragBit:Bitmap;
				
				if(cell[IS_PRE])
				{
					var obj:ObjectData=cell[DATA];
					if(isCheck)
					{
						if(obj && steerStep==-1)
						{
							if(cell["nx"])
								cell["nx"].visible =false;
							steerStep =5;
							administerHanle(steerStep,cell)
						}
					}
					
					SwfLoader.StartLoad(ResourcesPath.getModel(obj), null, loadOver);
					function loadOver(mc:*,pL:Array):void
					{
						var bitmap:Bitmap=BitmapAPI.getBitmap(mc);
						bitmap.scaleX = bitmap.scaleY =  0.8 ; 
						StageInfo.stage.addChild(bitmap);
						var point:Point=cell.parent.localToGlobal(new Point(cell.x,cell.y));
						bitmap.x=point.x;
						bitmap.y=point.y;
						dragBit=bitmap;
						beginDrag();
						
					}
				}
				else
				{
					dragBit=KungfuPackLogic.getInstance().getBitMapInSamePoint(cell[CELL]);
					dragBit.scaleX = dragBit.scaleY = 0.8; 
					if(!dragBit)return;
					if(isCheck)  //点击拖动 检查是否需要引导
					{
						if(cell[DATA] is RoleVo)
						{
							if(!rolePos)
							{
								steerStep=2;
								
								administerHanle(steerStep,cell);
							}
							else
							{
								
							}
						}
						else
						{
							if(!buddyPos)
							{
								steerStep=5;
							
								administerHanle(steerStep,cell);
							}
							else 
							{
								trace("禁止")
							}
						}
					}
					beginDrag();
				}
			}
			function beginDrag():void
			{
				if(_isFrist&&cell[DATA])
				{
					
				}
				//				_mouseHandle.setDrag(cell[CELL].content,true,[sourceArr,cell]);
				_mouseHandle.setMouseDrag(dragBit,true,[sourceArr,cell] ,0.5 , 1);
				dragBit.parent.removeChild(dragBit);
				ui.preEmbaGroup.addItemsEvent(MouseEvent.MOUSE_UP,mouseUpHandle)
//								ui.emBattleGroup.addItemsEvent(MouseEvent.MOUSE_UP,mouseUpHandle)
				emBattleItemEachFunc(it)
				StageInfo.stage.addEventListener(MouseEvent.MOUSE_UP,stageMouseUpHandle);
			}
			 function it(mc:MovieClip,index:int):void
			{
				mc.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandle);
			}
		}

		private function listenerRemove(mc:MovieClip,index:int):void
		{
			mc.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandle);
		}
		private function mouseUpHandle(e:Event):void
		{
			
			var cell:MovieClip=e.currentTarget as MovieClip; 
			var value:int =cell.name.indexOf("ItemEffect_");
			if(value!=-1)
			{
				var index :int  = cell.name.split("ItemEffect_")[1];
				cell = getEmbattleItemByindex(index);
			}
			ui.preEmbaGroup.removeItemsEvent(MouseEvent.MOUSE_UP,mouseUpHandle);
//			ui.emBattleGroup.removeItemsEvent(MouseEvent.MOUSE_UP,mouseUpHandle);
			emBattleItemEachFunc(listenerRemove)
			StageInfo.stage.removeEventListener(MouseEvent.MOUSE_UP,stageMouseUpHandle);
//			if(ui.emBattleGroup.contains(cell)&&cell.currentFrame	==	LOCK_FRAME)
//			{
////				AlertUI.showAlert("警告","不能在该位置上阵！");	
//				return;
//			}
			if(_mouseHandle.targetData == null )return  ; 
			
			var sourcecell:MovieClip=_mouseHandle.targetData[1] as MovieClip;
			var indexx:int=sourcecell[INDEX];
			var sourceArr:Array=_mouseHandle.targetData[0] as Array;
			_mouseHandle.removeDrag();
			//交换
			e.stopImmediatePropagation();
//			if(sourceArr	==	_prearr	&&	ui.emBattleGroup.contains(cell)	&&	MAX_EMBA_COUNT-_nowCountInEmbattle	<=	0	&&	!cell[DATA])
//			{
////				AlertUI.showAlert(StaticIni.getSwfLangStr("BuZhenStr_19"),StaticIni.getSwfLangStr("BuZhenStr_20"));
//				return ;
//			}
//			else if(cell[DATA]	==	_role &&ui.preEmbaGroup.contains(sourcecell))
//			{
////				AlertUI.showAlert(StaticIni.getSwfLangStr("BuZhenStr_17"),StaticIni.getSwfLangStr("BuZhenStr_18"));
//				return ;
//			}
			/*else*/ if( sourceArr== _emArr &&!(cell[IS_PRE]==false))  //下阵
			{
				removeEmbaCell(sourcecell[INDEX]);
				if(isCheck)
				{
					if(sourcecell[DATA] is RoleVo)
					{
						steerStep =1;
						administerHanle(steerStep,_currRolemc)
					}
					else
					{
						_buddyIsRight=true;
						if(steerStep>=4)
						{
							if(!rolePos)
							{
								steerStep=1;
								administerHanle(steerStep,_currRolemc)
							}
							else
							{
								if(currpos!=null)
									currpos["nx"].visible=false;
								currpos=null
							}
						}
						
						
					}
				}
			}
			else if(sourceArr!=_prearr	||	!ui.preEmbaGroup.contains(cell))  //上阵
			{
				swapKit(sourcecell,cell)
				if(isCheck)
				{
					if((steerStep ==2 || steerStep ==5) && sourcecell[INDEX] == cell[INDEX])  // 没有移动
					{
						steerStep--;
						administerHanle(steerStep,sourcecell);
					}
//					else if((sourcecell[INDEX]==4 ||sourcecell[INDEX]==5) && (cell[INDEX] ==4 || cell[INDEX]==5))
//					{
//						
////						if(sourcecell.isMe)
////						{
//							steerStep=1;
////						}
////						else
////						{
////							steerStep=4;
////						}
////						
//						administerHanle(steerStep,cell,true);
//					}
					else
					{
						if(_ui.embattle["embItem_"+5][DATA]&& cell ==_ui.embattle["embItem_"+5]&& _isFrist)
						{
							steerStep=-1;
							administerHanle(steerStep,sourcecell);
							return;
						}
						resetAdminister(sourcecell[DATA],cell);
						checkNext(sourcecell[DATA])
						if(_isFrist)
						{
							_isFrist=false;
						}
					}
				}
			}
			else if(ui.preEmbaGroup.contains(cell) && _isFrist)  //第一个打开
			{
				steerStep =-1
				administerHanle(steerStep, _prearr[0])
			}
//			if(sourceArr==_prearr	&&	!ui.preEmbaGroup.contains(cell) )
//			{
//				clearItem(sourcecell)
//			}
		}
		/**
		 *检查下一个 
		 * @param viewobj
		 * 
		 */		
		private function checkNext(viewobj:*):void
		{
			if(viewobj is RoleVo)
			{
				if(!buddyPos)
				{
					steerStep=4;
					
				}
				else
				{
					steerStep=0;
				}
				administerHanle(steerStep,_currBuddymc);
			}
			else if(viewobj is ViewObject)
			{

				 if(!rolePos)
				{
					steerStep=1;
					
				}
				else
				{
					steerStep=0;
				}
				 administerHanle(steerStep,_currRolemc);
			}
		
		}
		/**
		 * 
		 * @param viewobj
		 * @param mc
		 * 
		 */		
		private function resetAdminister(viewobj:*,mc:MovieClip):void
		{
			if(viewobj is RoleVo)
			{
				if(!rolePos)
				{
					steerStep=1;
					administerHanle(steerStep,mc);
				}
				else if(!buddyPos)
				{
					steerStep=4;
					administerHanle(steerStep,_currBuddymc);
				}
				else
				{
					steerStep=0;
					administerHanle(steerStep)
				}
			
			}
			else if(viewobj is ViewObject)
			{
				 if(!buddyPos)
				 {
					 steerStep=4;
					 administerHanle(steerStep,mc);
				 }
				 else if(!rolePos)
				 {
					 steerStep=1;
					 administerHanle(steerStep,_currRolemc);
				 }
				 else
				 {
					 steerStep=0;
					 administerHanle(steerStep)
				 }
			}
			
		}
		private function stageMouseUpHandle(e:Event):void
		{
			ui.preEmbaGroup.removeItemsEvent(MouseEvent.MOUSE_UP,mouseUpHandle)
//			ui.emBattleGroup.removeItemsEvent(MouseEvent.MOUSE_UP,mouseUpHandle)
			emBattleItemEachFunc(listenerRemove)
			StageInfo.stage.removeEventListener(MouseEvent.MOUSE_UP,stageMouseUpHandle);
			if(!_mouseHandle.targetData||!_mouseHandle.targetData.hasOwnProperty(1)||!_mouseHandle.targetData.hasOwnProperty(0))return ;
			var sourcecell:MovieClip=_mouseHandle.targetData[1] as MovieClip;
			var indexx:int=sourcecell[INDEX];
			var sourceArr:Array=_mouseHandle.targetData[0] as Array;
			_mouseHandle.removeDrag();
			//移除
			if(sourcecell[IS_PRE])
			{
				if(_isFrist)
				{
					steerStep=-1;
					administerHanle(steerStep,sourcecell);
				}
				return ;
			}
			removeEmbaCell(sourcecell[INDEX])
			if(isCheck)
			{
				if(sourcecell[DATA] is RoleVo) //主角不能下阵
				{
					if(!rolePos)
					{
						steerStep=1;
						administerHanle(steerStep,_currRolemc);
					}
				}
				else
				{
					
					if(!rolePos)
					{
						administerHanle(1,_currRolemc)
					}
					else
					{
						if(currpos!=null)
							currpos["nx"].visible=false;
						currpos=null
					}
				}
			}
			refreshAfterChange();
		}
		
		private function OnAddPartner(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			_roleArr[int(PropertiesValue.ObjKey)-1]=PropertiesValue;
			if(PropertiesValue.ObjKey)
			{
				_partnerUidMap[PropertiesValue.ObjKey]=PropertiesValue;
			}
//			addItem(_prearr[int(PropertiesValue.ObjKey)-1],PropertiesValue);
			_real_Map[PropertiesName]	=	_preLastFlag;
			addItem(_prearr[_preLastFlag++],PropertiesValue);
		}
		private function OnRemovePartner(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			_roleArr[int(PropertiesValue.ObjKey)-1]=null;
			clearItem(_prearr[_real_Map[int(PropertiesValue.ObjKey)]])
		}
		private function doubleClickHandle(e:MouseEvent):void
		{
			var value:int =e.target.name.indexOf("ItemEffect_");
			if(value!=-1)
			{
				var index :int  = e.target.name.split("ItemEffect_")[1];
			}
			var emCell:MovieClip=findCell(getEmbattleItemByindex(index) as DisplayObject);
			removeEmbaCell(emCell.index);
			refreshAfterChange();
			if(emCell && emCell[DATA])
			{
				if(emCell[DATA] is RoleVo)return;
				if(!rolePos)
				{
					administerHanle(1,_currRolemc)
				}
				else
				{
					if(currpos!=null)
						currpos["nx"].visible=false;
					currpos=null
				}
			}
			
		}
		private function clearAllPartner():void
		{
			for each (var i:MovieClip in _prearr) 
			{
				clearItem(i)
			}
			_roleArr=[];
			var mc:MovieClip;
			var len:int= _ui.preEmbaGroup.row*_ui.preEmbaGroup.col;
			for (var ii:int = 0; ii <len; ii++) 
			{
				mc	=	_ui.preEmbaGroup.getItemByIndex(ii) as MovieClip
				if(ii==0)
				{
					mc["nx"].visible=false;
				}
				//				mc.stop()
			}

		}
		private function clearItem(mc:MovieClip):void
		{
//			mc[INDEX]=0;
			if(!mc)return ;
			mc[CELL].clear();
			mc[CELL].source=null;
//			mc[PRIO_LABEL].;
			if(mc[PRIO_LABEL])
			mc[PRIO_LABEL].visible=false;
			mc[DATA]=null;
			mc[IS_LOADING]="";
			mc.isMe=false;	
			if(mc.hasOwnProperty(BG))
			{
				mc[BG].visible=false;
			}
//			if(mc[CELL].content.hasOwnProperty(NAME))
//				mc[CELL].content[NAME].text=="";
		}
	
		private var parnetModelArr:Array=[];
		public function loadAllModel():void
		{
			var url:String =ResourcesPath.getModel(_role as SceneObject,true);
			if(url &&!urlObj.hasOwnProperty(url))
				parnetModelArr.push(url);
			var view:View = _model.scene.GetView(View.VIEWPORT_PARTNER);
			if(view !=null)
			{
				for (var property:String in view.Properties) 
				{
					if(int(property))
					{
						if(view.CheckProperties(property))
						{
							var item:ViewObject=view.GetPropertiesEx(property);
							if(item.CheckProperties("Model"))
							{
								url =ResourcesPath.getModel(item as ObjectData,true)
								if(url &&!urlObj.hasOwnProperty(url))
									parnetModelArr.push(ResourcesPath.getModel(item as ObjectData,true));
							}
			
						}
						
					}
				}
				//打开UI
				load(parnetModelArr);
			}
		}
		private var Flag:int=0;
		private function load(arr:Array):void
		{
			if(arr.length>0)
			{
				MultLoader.getInstance().load(arr.concat(), true, loadOver);
			}
			else
			{
				_ui.showUI();
			}
		}
		private function loadOver():void
		{
			var mc:MovieClip;
			for(var i:String  in  parnetModelArr)
			{
				 mc =  MultLoader.getInstance().getSourceByUrl(parnetModelArr[int(i)]);
				 if(mc)
				 {
					 urlObj[parnetModelArr[int(i)]] =mc;
				 }
			}
			_ui.showUI();
		}
		private var urlObj:Object ={};
		private function checkHaveModel(url:String):MovieClip
		{
			for(var i:String in urlObj)
			{
				if(i ==url)
				{
					return urlObj[i];
				}
			}
			return null;
			
		}
		/**
		 *加载人物模型 
		 * @param mainMc
		 * @param url
		 * 
		 */		
		private function loadModel(mainMc:MovieClip,url:String):void
		{
			if(	mainMc[IS_LOADING]==url)
			{
				return ;
			}

			if(checkHaveModel(url))
			{
				var mc:MovieClip = urlObj[url];
				if(mc.numChildren == 1 && mc.totalFrames == 1 )
				{
					mc = mc.getChildAt( 0 ) as MovieClip;
				}else
				{
					mc = ResManager.copy( mc ) as  MovieClip;
				}
				mc.gotoAndPlay(PlayerActionFlag.Wait);
				mainMc[CELL].source =urlObj[url];
				return;
			}
			mainMc[IS_LOADING]=url;
			SwfLoader.StartLoad(url, null, loadOver,[mainMc]);
			 function loadOver(mc:*,pL:Array):void
			 {
				 if(mc.numChildren == 1 && mc.totalFrames == 1 )
				 {
					 mc = mc.getChildAt( 0 ) as MovieClip;
				 }else
				 {
					 mc = ResManager.copy( mc ) as  MovieClip;
				 }
				 mc.gotoAndPlay(PlayerActionFlag.Wait);
				 if(url==ResourcesPath.getModel(mainMc[DATA],true))
				 	mainMc[CELL].source=mc;
			 }
		}
		/**
		 * 
		 * @param mc
		 * @param datao
		 * @param isPre 是不是在预备里
		 * 
		 */		
		private function addItem(mc:MovieClip,datao:*):void
		{
		//			trace("EmbattleLogic.addItem(mc, datao)",datao.GetProperties("Photo"),datao==_role);
			mc[DATA]=datao;
			if(mc[IS_PRE])
				mc[CELL].source=ResourcesPath.getMiniHeadPic((datao as ObjectData).GetPropertiesEx("Model"),datao.GetPropertiesEx("ClotheModel"));
			else
			{
				loadModel(mc,ResourcesPath.getModel(datao as ObjectData,true));
//				mc[CELL].source	=ResourcesPath.getModel(datao as ObjectData);//ResourcesPath.getHeadPic((datao as ObjectData).GetPropertiesEx("Model"),_role==datao?_role.clotheModel:0);//ResPath.JPG + "caption_.jpg";
			}
//				trace("addItem",mc[CELL].source);
//			trace("EmbattleLogic.addItem(mc, datao)",datao.GetProperties("Photo"),datao==_role);

			(mc[CELL] as DisplayObject).parent.setChildIndex(mc[CELL],(mc[CELL] as DisplayObject).parent.numChildren-1);
				//String(datao.GetProperties("Photo")?datao.GetProperties("Photo"):datao.GetProperties("Model")) 
				
			if(datao==_role)
			{
				mc.isMe=true;

			}
			else
			{
				mc.isMe=false;	
			}
			
			if(mc[IS_PRE]	==	false)
			{
				mc[BG][NAME].text		=	datao is ViewObject?StaticIni.getSwfLangStr(datao.GetPropertiesEx("ConfigID")):datao.GetPropertiesEx("Name");
				mc[BG].visible	=	true;
				mc[PRIO_LABEL].visible	=	radioSelect;
//				updatesteer(mc["index"],mc.isMe)
			}
		}
		private function radioHandle(e:Event):void
		{
			//			e.nxRadio.selected=!e.nxRadio.selected;
			radioSelect=!radioSelect
			updatPriority()
		}
		private function uplevelhandle(e:MouseEvent):void
		{
//			trace("EmBattleUI.uplevelhandle(e)升级");
//			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_UPGRADE_LINEUP);
			StatusShopUI.getInstance().showOrHide();
			
		}
		/**
		 *启用阵法 
		 * @param e
		 * 
		 */		
		private function evokehandle(e:MouseEvent):void
		{
//			upBattle=(_ui.battleList.currentSelect)
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHANGE_LINEUP_ID,currentSeleted+1);
			
		}
		/**
		 *启用阵法 
		 * @param index
		 * 
		 */		
		public function set upBattle(index:int):void
		{
			if(index==0)
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHANGE_LINEUP_ID,1);
				return ;
			}
			var index:int=index-1;
			var mc:MovieClip=_listArr[index] as MovieClip;
			mc.gotoAndStop(EVOKE);
			if(_past_flag==index)return;
			if(_past_flag>=0)
			{
				mc=_listArr[_past_flag]//_ui.battleList.getItemByIndex(_past_flag);
				if(mc.currentFrame==EVOKE)
				{
					mc.gotoAndStop(NORMAL);
				}
			}
			_past_flag=index;
			updataTxt();
			//强制刷新
//			_ui.battleList.iconOffSizeY=_ui.battleList.iconOffSizeY;
		}
		private function swapKit(sCell:MovieClip,tCell:MovieClip):void
		{
			if(sCell==tCell	/*||	(tCell[DATA]	==	_role	&&	ui.preEmbaGroup.contains(sCell))*/)
				return ;
			var sindex:int=sCell[INDEX];
			var tIndex:int=tCell[INDEX];
//			trace("sCell[DATA].ObjID",sCell[DATA].ObjID)
			var lineUp:int;
			if(!sCell[IS_PRE])
			{
				if(	!tCell[IS_PRE])
				{
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_EXCHANGE_LINEUP_POS,currentSeleted+1,sindex,tIndex);					
				}
				else if(tCell[DATA])
				{
					removeEmbaCell(tIndex)
				}
			}
			else
			{
				if(tCell[DATA])
				{
					
					removeEmbaCell(tCell[INDEX]);
					//					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ADD_TO_LINEUP_POS,currentSeleted+1,getTargetObjKey(tCell[DATA]),tIndex);					
					
				}
				if(!tCell[IS_PRE])
				{
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ADD_TO_LINEUP_POS,currentSeleted+1,getTargetObjKey(sCell[DATA]),tIndex);	
//					removeEmbaCell(tCell[INDEX]);
				}
			
					
//				else
//				{
//					
//				}
			}
			
//			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHANGE_LINEUP_POS,currentSeleted+1,int(getTargetObjKey(sCell[DATA])),tIndex);
//			if(tCell[DATA])
//			{
//				if(sCell[IS_PRE])
//				{
//					removeEmbaCell(tCell[INDEX])
//				}
//				else
//				{
//					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_EXCHANGE_LINEUP_POS,currentSeleted+1,int(getTargetObjKey(tCell[DATA])),sindex);
//				}
//			}
			function getTargetObjKey(data:Object):int
			{
				if(data	!=	_role)
				{
					return data.ObjKey;
				}
				return -1
				
			}
		}
		/**
		 *删除上阵元素方法 
		 * @param index 索引
		 * 
		 */		
		private function removeEmbaCell(index:int):void
		{
			var tmc:MovieClip=_emArr[index]//groupem.remove(index);
				
			_waitingforUpdata=true
			if(tmc)
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REMOVE_FROM_LINEUP_POS,
					currentSeleted+1,index);
			}
		}
		private function findPreFirstEmpty():MovieClip
		{
			for each (var i:MovieClip in _prearr) 
			{
				if(!i[DATA])
					return i
			}
			return null;
		}
		/**
		 *阵法变化后的刷新方法 
		 * 
		 */		
		private	function refreshAfterChange():void
		{
//			saveData()
			updatPriority()
			
		}
		/**
		 *遍历显示节点树 
		 * @param dp
		 * @return 
		 * 
		 */		
		private function findCell(dp:DisplayObject):MovieClip
		{
			var mc:MovieClip;
			for (var i:int = 0; i < _prearr.length; i++) 
			{
				 mc=_prearr[i];
				if(mc==dp||mc.contains(dp))
				{
					return mc
				}
			}
			for ( i = 0; i < _emArr.length; i++) 
			{
				 mc=_emArr[i];
				if(mc==dp||mc.contains(dp))
				{
					return mc
				}
			}
			return null;
		}
		public function afterHide():void
		{
			parnetModelArr=[];
			_preLastFlag=0;
			clearAllPartner();
			removeAllListener();
			_buddyIsRight=true;
			_roleIsRight=true;
			_isFrist=false;
			steerStep =0;
			if(currpos)
			{
				currpos["nx"].visible=false;
				currpos=null;
			}
				
		}
		private function onClickList(e:Event):void
		{
			var mc:MovieClip=e.currentTarget as MovieClip;
			if(mc.currentFrame==DISABLE)
				return ;
			currentSeleted=_listArr.indexOf(e.currentTarget);
			update();
		}
		//根据选择阵法更新  阵法
		private function update():void
		{
			var dataarr:Array=_tabArr[currentSeleted]
			if(!dataarr||!dataarr.length)
			{
				dataarr=_tabArr[currentSeleted]=[];
				preObjArr=_roleArr;
			}
			else
			{
				var preObjArr:Array=[];
				for (var i:int = 0; i < _roleArr.length; i++) 
				{
					var voo:ViewObject=_roleArr[i];
					if(!voo)continue;
					var tempArr:Array=dataarr.filter(isEqual);
					if(!tempArr||!tempArr.length)
					{
//						trace("EmbattleLogic.update()",voo.GetPropertiesEx("Name"));
						_real_Map[voo.ObjKey]=preObjArr.length
						preObjArr.push(voo);
					}
					else
					{
//						preObjArr.push(null);
					}
				}
			}
			_preLastFlag=preObjArr.length;
			updataLock();
			updaIt(preObjArr,dataarr);
			updatPriority();
			function isEqual(element:*, index:int, arr:Array):Boolean {
				if(element)
					return (element).GetProperties("ConfigID")	==	voo.GetProperties("ConfigID");
				return false
			}
		}
		/**
		 *阵法上锁（现在仿佛没有 ） 
		 * 
		 */		
		private function updataLock():void
		{
			if(!_currentUnLockarr)
			{
//				trace("EmbattleLogic.updataLock()",null);
				return 
			}
			for (var i:int = 0; i < _emArr.length; i++) 
			{
				var mc:MovieClip=_emArr[i];
				if(_currentUnLockarr.indexOf(String(i))	>=	0)
				{
					mc.gotoAndStop(BATT_NORMAL);
				}
				else
				{
					mc.gotoAndStop(LOCK_FRAME);
				}
			}
		}
		/**
		 *引导动画后调用 
		 * 
		 */		
		public function guideHook():void
		{
			if(_prearr[0][DATA])
			{
				swapKit(_prearr[0],_emArr[3]);
			}
		}
		/**
		 *刷新文本 
		 * 
		 */		
		private function updataTxt():void
		{
			_nowCountInEmbattle	=	0;
			var prioCount:int
			for each (var i:MovieClip in _emArr) 
			{
				if(i&&i[DATA])
				{
					_nowCountInEmbattle++;
					var vo:ObjectData	=	i[DATA];
//					trace("updtatext",vo.CheckProperties("SpeedRate"),vo.GetProperties("SpeedRate"))
//					trace("updtatext",vo.CheckProperties("SpeedRateAdd"),vo.GetPropertiesEx("SpeedRateAdd"))
//					prioCount+=vo.GetProperties("SpeedRate");
//					prioCount+=vo.GetPropertiesEx("SpeedRateAdd");
//					prioCount=prioCount*(1 + (vo.GetPropertiesEx("SpeedRateMul") + vo.GetPropertiesEx("SpeedRateMulAdd")) / 100);
//					prioCount=vo.GetPropertiesEx("TotalSpeedRateAdd")
				}
			}
			proirityCount	=	_role.getSpeedRate()//prioCount//+currentbattleSpeed();
			embattleCount	=	MAX_EMBA_COUNT-_nowCountInEmbattle;
			
		}
		
		/**
		 * 可上阵人数
		 * @param value
		 * 
		 */
		private function get embattleCount():int
		{
			return _embaCount;
		}
		private function set embattleCount(value:int):void
		{
			_embaCount	=	value;
			if(value>0)
				_ui.embattlePersonLab.htmlStrByBorder	=	StaticIni.getSwfLangStrVar("BuZhenStr_12",[value])//"可上阵人数"+value+"人";
			else
				_ui.embattlePersonLab.htmlStrByBorder	=	StaticIni.getSwfLangStr("BuZhenStr_13")//"上阵人数已满";
		}
		/**
		 *刷新出手优先顺序 
		 * 
		 */		
		private function updatPriority():void
		{
			//			if(rankRadio.selected)
			_priorityFlag	=	0;
			for each (var i:int in rank) 
			{
				if(_emArr[i][DATA])
				{
					setPriority(i);
				}
				else
				{
					clearPriority(i)
				}
			}
			updataTxt();
		}
		private function setPriority(index:int):void
		{
			var cell:MovieClip	=	_emArr[index] as MovieClip;
			++_priorityFlag;
//			cell[PRIORITY].text	=	StaticIni.getSwfLangStr(toChin[_priorityFlag])+StaticIni.getSwfLangStr("BuZhenStr_14");
			cell[PRIO_LABEL].visible	=	true&&radioSelect;
			cell[PRIO_LABEL].gotoAndStop(_priorityFlag);
		}
		private function clearPriority(index:int):void
		{
			var cell:MovieClip=_emArr[index] as MovieClip;
//			cell[PRIORITY].text="";
			cell[PRIO_LABEL].visible=false
		}
		private function updaIt(pArr:Array,dArr:Array):void
		{
			_nowCountInEmbattle	=	0;
			var mc:MovieClip;
			for (var i:int = 0; i < _prearr.length; i++) 
			{
				mc=_prearr[i] as MovieClip;
				if(i<pArr.length&&pArr[i])
				{
					addItem(mc,pArr[i]);
				}
				else
				{
					clearItem(mc);
				}
			}
				
			for ( i = 0; i < _emArr.length; i++) 
			{
				mc	=	_emArr[i] as MovieClip;
				if(i<dArr.length&&dArr[i])
				{
					_nowCountInEmbattle++;
					addItem(mc,dArr[i]);
					//在这掉用 检查玩家是否在正确位置
//					updatesteer(i,dArr[i]);
				}
				else
				{
					clearItem(mc);
				}
			}
		}
		/**
		 *得到总共阵法数 
		 * @return 
		 * 
		 */		
		public function get battleLen():int
		{
			return 8
		}
		public function set level(value:int):void
		{
			var lvl:int= StaticIni.getIniVar("GuideInfo","levelSort").split(",")[_upBtnGuideIndex-1];
			ui.uplevelBtn.visible	=	value>=lvl;
		}
		/**
		 *设置阵法等级 
		 * @param value
		 * @return 
		 * 
		 */		
		public function set battlelevel(value:int):void
		{
			_battleLvl=value;
			_ui.levelLab.htmlStrByBorder	=	StaticIni.getSwfLangStrVar("BuZhenStr_15",[value])//"阵法等级"+value+"";
			_currentConfigArr=getConfigArr(value);
			updataTxt();
		}
		public function get proirityCount():int
		{
			if(_ui.created)
			return _proirity
			BeforeShow();
			var i:int=_proirity
			afterHide()
			return i;
		}
		/**
		 *设置先攻值 
		 * @param value
		 * 
		 */		
		public function set proirityCount(value:int):void
		{
			_proirity=value;
			_ui.priorityLab.htmlStrByBorder	=	StaticIni.getSwfLangStrVar("BuZhenStr_16",[value])//"先攻值"+value+"";
		}
		private function onClickClose(e:Event):void
		{
			_ui.showOrHide();
		}
	}
}